package com.ding.login.service.impl;

import com.ding.login.entity.RecordLogin;
import com.ding.login.mapper.RecordLoginMapper;
import com.ding.login.service.IRecordLoginService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ding.login.util.RequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Ding
 * @since 2022-07-21
 */
@Service
public class RecordLoginServiceImpl extends ServiceImpl<RecordLoginMapper, RecordLogin> implements IRecordLoginService {

    @Autowired
    private HttpServletRequest request;

    @Override
    public void saveLoginLog(Integer userId) {

        RecordLogin recordLogin = new RecordLogin();

        LocalDateTime localDateTime=LocalDateTime.now();
        Date date = Date.from(localDateTime.atZone( ZoneId.systemDefault()).toInstant());

        recordLogin.setTime(date);

        recordLogin.setId(userId);

        recordLogin.setDistrict("深圳蜗牛");

        //获取ip
        recordLogin.setIp(RequestUtil.getIPAddress(request));

        //获取浏览器信息
        recordLogin.setBrowser(RequestUtil.getOsAndBrowserInfo(request));

        this.saveOrUpdate(recordLogin);
    }
}
